package com.oco.configuration.mybatis;

import java.util.Properties;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Config;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;


/**
 * MyBatis扫描接口
 * 
 * @author ZhiHong Zhang
 * @email it_javasun@yeah.net
 * @sine 上午11:17:46
 */
@Configuration
//TODO 注意，由于MapperScannerConfigurer执行的比较早，所以必须有下面的注解
@AutoConfigureAfter(MyBatisConfig.class)
public class MyBatisMapperScannerConfig {
	
	@Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setBasePackage("com.oco.mybatis.dao");
        
        //设置配置
        MapperHelper mapperHelper = new MapperHelper();
        //特殊配置
        Config config = new Config();
		//具体支持的参数看后面的文档
        // config.setUUID(java.util.UUID.randomUUID().toString().replace("-", ""));
        Properties properties = new Properties();
        properties.setProperty("mappers", "com.oco.configuration.mybatis.MybatisMapper");
        properties.setProperty("notEmpty", "false");
        properties.setProperty("IDENTITY", "MYSQL");
        config.setProperties(properties);
        mapperHelper.setConfig(config);
        // 注册自己项目中使用的通用Mapper接口，这里没有默认值，必须手动注册
        mapperHelper.registerMapper(Mapper.class);
        mapperScannerConfigurer.setMapperHelper(mapperHelper);
        return mapperScannerConfigurer;
    }
}
